package com.richapp.launcher.database.infrared.dao;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.GenericRawResults;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.richapp.launcher.database.infrared.InfraredDatabaseHelper;
import com.richapp.launcher.database.infrared.mode.Brand;

import java.util.ArrayList;
import java.util.List;

public class BrandDao {

    private Context context;
    private InfraredDatabaseHelper helper = null;
    private Dao<Brand, Integer> dao = null;

    public BrandDao(Context context) {
        this.context = context;
        try {
            helper = InfraredDatabaseHelper.getHelper(this.context);
            dao = helper.getDao(Brand.class);
            dao.setObjectCache(false);
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
    }

    public Brand get(String brandName) {
        Brand brand = null;
        try {
            QueryBuilder queryBuilder = dao.queryBuilder();
            queryBuilder.where().eq("brandCh", brandName);
            Object obj = queryBuilder.queryForFirst();
            if (obj instanceof Brand) {
                brand = (Brand) obj;
            }
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
        return brand;
    }

    public Brand get(int brandId) {
        Brand brand = null;
        try {
            QueryBuilder queryBuilder = dao.queryBuilder();
            queryBuilder.where().eq("brandId", brandId);
            Object obj = queryBuilder.queryForFirst();
            if (obj instanceof Brand) {
                brand = (Brand) obj;
            }
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
        return brand;
    }

    public List<Brand> getList() {
        List<Brand> list = null;
        try {
            list = dao.queryBuilder().orderBy("brandCh", true).query();
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
        return list;
    }

    public List<Brand> getList(int machineTypeId) {
        List<Brand> list = new ArrayList<Brand>();
        try {
            String sql = "select brand.brandId,brand.brandCh,brand.brandEn,brand.note from brand where brandId in(select DISTINCT(brandId) from brandModel where machineTypeId = ?) order by brandCh";
            GenericRawResults<String[]> rawResults = dao.queryRaw(sql, machineTypeId + "");
            List<String[]> resultStrs = rawResults.getResults();
            Brand brand = null;
            for (String[] colums : resultStrs) {
                brand = new Brand();
                brand.setBrandId(Integer.parseInt(colums[0]));
                brand.setBrandCh(colums[1]);
                brand.setBrandEn(colums[2]);
                brand.setNote(colums[3]);
                list.add(brand);
            }
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
        return list;
    }

    public void add(Brand brand) {
        try {
            dao.createOrUpdate(brand);
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
    }

    public void deleteAll() {
        try {
            DeleteBuilder deleteBuilder = dao.deleteBuilder();
            deleteBuilder.delete();
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
    }
}
